package tech.aistar.corejava.day12.dayhomeork;

import tech.aistar.corejava.day05.homework03.Product;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * Created by Administrator on 2019/7/30.
 */
public class TreeSetWork {
    public static void main(String[] args) {
        TreeSet<Product> products = new TreeSet<>(new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                if (o1.getPrice()<o2.getPrice())
                    return -1;
                else if (o1.getPrice() == o2.getPrice()){
                    if (o1.getName().charAt(0)<o2.getName().charAt(0))
                        return -1;
                    else {
                        if (o1.getId() < o2.getId())
                            return -1;
                        else
                            return 1;
                    }
                }
                return 1;
            }
        });
        Product p1 = new Product(1, "小米", 10.00, "河南");
        Product p2 = new Product(2, "大米", 10.00, "河南");
        Product p3 = new Product(3, "绿豆", 20.00, "河南");
        Product p4 = new Product(4, "绿豆", 20.00, "河南");
        Product p5 = new Product(5, "阿幽", 30.00, "河南");

        products.add(p1);
        products.add(p2);
        products.add(p3);
        products.add(p4);
        products.add(p5);
        System.out.println(products);
        System.out.println("========================");
        Iterator<Product> iter = products.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
        }
        System.out.println("===========================");
        products.forEach(System.out::println);
    }
}
